package com.tqfframe.task.learning.threadStudy;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: 唐祺峰
 * @Date: 2020/9/9 0009 14:41
 * @Version 1.0
 */
public class FirstThread {
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();
    public static void main(String[] args) throws InterruptedException {

        lock.lock();
        new Thread(new SignalThread(),"t1").start();
        new Thread(new SignalThread(),"t2").start();
        System.out.println("等待通知");
        try {
            condition.await();
        } finally {
            lock.unlock();
        }
        System.out.println("我已经执行完线程了，因此我恢复运行");
    }
    static class SignalThread implements Runnable {

        @Override
        public void run() {
            lock.lock();
            try {

                try {
                    //模拟一个任务con
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                condition.signal();
                System.out.println(Thread.currentThread().getName()+"通知");
            } finally {
                lock.unlock();
            }
        }
    }
}
